<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.conn.RecordSetDataSource" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.*" %>
<%@ page import="weaver.general.BaseBean" %>
<%
    out.print( ">>>>>>>同步开始>>>>>>>>");
    String departmentid_e9 = "12";
    String subcompanyid1_e9 = "33";
    String roleid_e9 = "40";
    String roleid_e8 = "48";
    RecordSetDataSource rsd = new RecordSetDataSource( "E8" );
    List<Map<String, Object>> mapList = new ArrayList<>();
    String workcodes = "";
    String sql = "select resourceid,rolelevel,orderby,resourcetype,alllevel," +
            "seclevelfrom,seclevelto,jobtitlelevel,subdepid,uuid from HrmRoleMembers  where roleid='"+roleid_e8+"'";
    rsd.executeSql( sql );
    while (rsd.next()) {
        String workcode = getE8WorkcodebyId( Util.null2String( rsd.getString( "resourceid" ) ) );//人员ID
        Map<String, Object> map = new HashMap<>();
        map.put("resourceid",Util.null2String( rsd.getString( "resourceid" ) ));
        map.put("rolelevel",Util.null2String( rsd.getString( "rolelevel" ) ));
        map.put("orderby",Util.null2String( rsd.getString( "orderby"  ) ));
        map.put("resourcetype",Util.null2String( rsd.getString( "resourcetype"  ) ));
        map.put("alllevel",Util.null2String( rsd.getString( "alllevel"  ) ));
        map.put("seclevelfrom",Util.null2String( rsd.getString( "seclevelfrom"  ) ));
        map.put("seclevelto",Util.null2String( rsd.getString( "seclevelto" ) ));
        map.put("jobtitlelevel",Util.null2String( rsd.getString( "jobtitlelevel" ) ));
        map.put("subdepid",Util.null2String( rsd.getString( "subdepid" ) ));
        map.put("uuid",Util.null2String( rsd.getString( "uuid" ) ));
        mapList.add(map);
        workcodes = "".equals( workcodes ) ? workcode : "," + workcode;
    }
    String sql1 = "select workcode from hrmresource where workcode not in (" + workcodes + ")";
    rs.executeQuery( sql1 );
    while (rs.next()) {
        String resourceid = getE9IdbyWorkcode( Util.null2String( rsd.getString( "workcode" ) ) );
        rs.executeUpdate( "delete HrmRoleMembers where resourceid=?", resourceid );
        rs.executeUpdate( "delete hrmresource where id=?", resourceid );
    }
    out.print( ">>>>>>>角色同步开始>>>>>>>>");
    String msg1 = hrmRolesyn(mapList,roleid_e9);
    out.print( ">>>>>>>角色同步信息："+msg1 );

    out.print( ">>>>>>>人员同步开始>>>>>>>>");
    String msg2 = hrmresourcesyn(workcodes,departmentid_e9,subcompanyid1_e9);
    out.print( ">>>>>>>人员同步信息："+msg2 );
%>
<%!
    public static String hrmresourcesyn(String workcodes,String departmentid,String subcompanyid1) {
        BaseBean baseBean = new BaseBean();
        RecordSetDataSource rsd = new RecordSetDataSource( "E8" );
        String sql = "select workcode,status,dsporder,mobile,password,lastname,loginid," +
                "systemlanguage,sex,birthday,lastmoddate,createdate,departmentid,subcompanyid1," +
                "seclevel,accounttype,belongto from hrmresource where workcode in (" + workcodes + ")";
        rsd.executeSql( sql );
        baseBean.writeLog( "查询sql"+sql );
        String msg = "";
        int counts = rsd.getCounts();
        int ucount = 0;
        int icount = 0;
        while (rsd.next()) {
            Map<String, Object> dataMap = new HashMap<>();
            String workcode = Util.null2String( rsd.getString( "workcode" ) );
            dataMap.put( "workcode", workcode );
            dataMap.put( "status", Util.null2String( rsd.getString( "status" ) ) );
            dataMap.put( "dsporder", Util.null2String( rsd.getString( "dsporder" ) ) );
            dataMap.put( "mobile", Util.null2String( rsd.getString( "mobile" ) ) );
            dataMap.put( "password", Util.null2String( rsd.getString( "password" ) ) );
            dataMap.put( "lastname", Util.null2String( rsd.getString( "lastname" ) ) );
            dataMap.put( "loginid", Util.null2String( rsd.getString( "loginid" ) ) );
            dataMap.put( "systemlanguage", Util.null2String( rsd.getString( "systemlanguage" ) ) );
            dataMap.put( "sex", Util.null2String( rsd.getString( "sex" ) ) );
            dataMap.put( "birthday", Util.null2String( rsd.getString( "birthday" ) ) );
            dataMap.put( "lastmoddate", Util.null2String( rsd.getString( "lastmoddate" ) ) );
            dataMap.put( "createdate", Util.null2String( rsd.getString( "createdate" ) ) );
            dataMap.put( "departmentid", departmentid);
            dataMap.put( "subcompanyid1", subcompanyid1);
            dataMap.put( "seclevel", Util.null2String( rsd.getString( "seclevel" ) ) );
            dataMap.put( "accounttype", Util.null2String( rsd.getString( "accounttype" ) ) );
            dataMap.put( "belongto", Util.null2String( rsd.getString( "belongto" ) ) );
            baseBean.writeLog( "dataMap="+dataMap.toString() );
            if (isExistHrm( workcode )) {//存在E8和E9存在相同人员，则更新该人员
                boolean update_success = updateData( "hrmresource", dataMap, workcode );
                baseBean.writeLog( "人员更新是否成功"+update_success );
                if(update_success){
                    ucount++;
                }
            } else {//存在E8和E9不存在相同人员，则插入新的人员
                boolean insert_success = insertData( "hrmresource", dataMap );
                baseBean.writeLog( "人员插入是否成功"+insert_success );
                if(insert_success){
                    icount++;
                }
            }
        }
        msg = "查询人员总数量："+counts+",人员更新成功数量"+ucount+",人员插入成功数量："+icount;
       return msg;
    }

    public static String hrmRolesyn(List<Map<String, Object>> mapList,String roleid){
        BaseBean baseBean = new BaseBean();
        String msg = "";
        int counts = mapList.size();
        int ucount = 0;
        int icount = 0;
        for(Map<String, Object> map :mapList){
            String workcode = getE8WorkcodebyId(Util.null2String(map.get("resourceid")));
            if (isExistHrm( workcode )) {//存在E8和E9存在相同人员，则更新该人员
                boolean update_success = updateData( "HrmRoleMembers", map, roleid );
                baseBean.writeLog( "角色更新是否成功"+update_success );
                if(update_success){
                    ucount++;
                }
            } else {//存在E8和E9不存在相同人员，则插入新的人员
                boolean insert_success = insertData( "HrmRoleMembers", map );
                baseBean.writeLog( "角色插入是否成功"+insert_success );
                if(insert_success){
                    icount++;
                }
            }
        }
        msg = "查询角色总数量："+counts+",角色更新成功数量"+ucount+",角色插入成功数量："+icount;
        return msg;
    }

    public static boolean isExistHrm(String workcode) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select id from hrmresource where workcode=?", workcode );
        rs.next();
        String E9id = Util.null2String( rs.getString( "id" ) );
        boolean flag = "".equals( E9id ) ? false : true;
        return flag;
    }

    /**
     * 更新数据
     * @param tableName 表名
     * @param map  数据集合
     * @param code 更新用唯一标识
     * @return
     */
    private static boolean updateData(String tableName, Map<String, Object> map, String code) {
        RecordSet rs = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String sql = "update " + tableName + " set  ";
        String valuesStr = "";
        Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            String key = Util.null2String( entry.getKey() );
            String value = Util.null2String( entry.getValue() );
            if (valuesStr.equals( "" )) {
                valuesStr = key + "=" + value + "";
            } else {
                valuesStr += "," + key + "=" + value + "";
            }
        }
        sql += valuesStr + " where ='" + code + "'";
        baseBean.writeLog( "updateData="+sql );
        boolean flag = rs.execute( sql );
        return flag;
    }

    /**
     * 新增数据
     * @param tableName 表名
     * @param map 数据集合
     * @return
     */
    private static boolean insertData(String tableName, Map<String, Object> map) {
        RecordSet rs = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String sql = "insert into  " + tableName;
        String keyString = "";
        String valueString = "";
        Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            if (keyString.equals( "" )) {
                keyString = entry.getKey();
            } else {
                keyString += "," + entry.getKey();
            }

            if (valueString.equals( "" )) {
                valueString = Util.null2String( entry.getValue() );
            } else {
                valueString += "," + entry.getValue();
            }

        }
        sql += "(" + keyString + ") values(" + valueString + ")";
        baseBean.writeLog( "insertData="+sql );
        boolean flag = rs.execute( sql );
        return flag;
    }

    /**
     * E8系统根据人员ID获取人员工号
     * @param resourceid 人员ID
     * @return 人员工号
     */
    private static String getE8WorkcodebyId(String resourceid) {
        RecordSetDataSource rsd = new RecordSetDataSource( "E8" );
        rsd.executeSql( "select workcode from hrmresource where id='" + resourceid + "'" );
        rsd.next();
        String workcode = Util.null2String( rsd.getString( "workcode" ) );
        return workcode;
    }
    /**
     * E9系统根据人员ID获取人员工号
     * @param resourceid 人员ID
     * @return 人员工号
     */
    private static String getE9WorkcodebyId(String resourceid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select workcode from hrmresource where id='" + resourceid + "'" );
        rs.next();
        String workcode = Util.null2String( rs.getString( "workcode" ) );
        return workcode;
    }
    /**
     * E8系统根据人员工号获取人员ID
     * @param workcode 人员ID
     * @return 人员ID
     */
    private static String getE8IdbyWorkcode(String workcode) {
        RecordSetDataSource rsd = new RecordSetDataSource( "E8" );
        rsd.executeSql( "select id from hrmresource where workcode='" + workcode + "'" );
        rsd.next();
        String id = Util.null2String( rsd.getString( "id" ) );
        return id;
    }
    /**
     * E9系统根据人员工号获取人员ID
     * @param workcode 人员ID
     * @return 人员ID
     */
    private static String getE9IdbyWorkcode(String workcode) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select id from hrmresource where workcode='" + workcode + "'" );
        rs.next();
        String id = Util.null2String( rs.getString( "id" ) );
        return id;
    }
%>